package com.nulldev.util.analytics.v2;

import java.util.HashMap;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.net.IPUtil;
import com.nulldev.util.os.BIOSUtil;
import com.nulldev.util.os.CPUUtil;
import com.nulldev.util.os.GPUUtil;
import com.nulldev.util.os.OSUtil;

public class nAnalytics2 {
	
	public static final String DEFAULT = "nAnalytics.null";
	public static final boolean DISABLED_HARD = JVMUtil.isArg("--nanalytics.disable");
	public static boolean DISABLED = DISABLED_HARD;

	// entry, data
	// i.e. os="Windows 98"
	private static final HashMap<Variables, String> data = new HashMap<Variables, String>();
	
	static {
		if (data.isEmpty()) {
			data.put(Variables.OS_TYPE, OSUtil.getOperatingSystemType().toString());
			data.put(Variables.OS_DETAILED, OSUtil.getOperatingSystemString());
			
			data.put(Variables.CPU, CPUUtil.parse().toString());
			data.put(Variables.GPU, GPUUtil.parse().toString());
			
			data.put(Variables.NETWORK, "{local=127.0.0.1,global=" + 
			IPUtil.getLocalHostLANAddress_safe() + ",internet=" + IPUtil.getPublicIP() + "}");
			
			data.put(Variables.BIOS, BIOSUtil.parse().toString());
		}
	}
	
	public static String getData(final Variables variable) {
		if (variable == Variables.OS_DETAILED || variable == Variables.OS_TYPE) {
			if (nAnalytics2Manager.canReadOS()) {
				final String var = data.get(variable);
				if (var == null) {
					return DEFAULT;
				}else {
					return var;
				}
			}
		}else if (variable == Variables.CPU) {
			if (nAnalytics2Manager.canReadCPU()) {
				final String var = data.get(variable);
				if (var == null) {
					return DEFAULT;
				}else {
					return var;
				}
			}
		}else if (variable == Variables.GPU) {
			if (nAnalytics2Manager.canReadGPU()) {
				final String var = data.get(variable);
				if (var == null) {
					return DEFAULT;
				}else {
					return var;
				}
			}
		}else if (variable == Variables.BIOS) {
			if (nAnalytics2Manager.canReadBIOS()) {
				final String var = data.get(variable);
				if (var == null) {
					return DEFAULT;
				}else {
					return var;
				}
			}
		}else if (variable == Variables.NETWORK) {
			if (nAnalytics2Manager.canAccessNetwork()) {
				final String var = data.get(variable);
				if (var == null) {
					return DEFAULT;
				}else {
					return var;
				}
			}
		}
		return DEFAULT;
	}
	
	public static enum Variables {
		OS_TYPE("os.type"),
		OS_DETAILED("os.detailed"),
		
		CPU("cpu"),
		GPU("gpu"),
		
		NETWORK("network"),
		BIOS("sys.bios"),
		
		;
		
		private final String var;
		private Variables(final String var) {
			this.var = var;
		}
		
		@Override
		public String toString() {
			return this.var;
		}
	}
}
